<?php
//Checkout -> 03 Bag
//checkout03bag.php

  if(!isset($_SESSION['shoppingBag'])) {
    $_SESSION['shoppingBag'] = array();
  }

  sort($_SESSION['shoppingBag']);
  $bagUnique = array_unique($_SESSION['shoppingBag']);
  $bagCount = array_count_values($_SESSION['shoppingBag']);

  $err = bag_operation($db, $_GET["action"], $_GET["pid"]);
  if ($err != "") {
    printAlert("error", "Oh snap!", $err, $_SERVER['PHP_SELF']);
  }

  sort($_SESSION['shoppingBag']);
  $bagUnique = array_unique($_SESSION['shoppingBag']);
  $bagCount = array_count_values($_SESSION['shoppingBag']);

  $bagProd = array();
  $bagProd = bag_construction($db);

  sort($_SESSION['shoppingBag']);
  $bagUnique = array_unique($_SESSION['shoppingBag']);
  $bagCount = array_count_values($_SESSION['shoppingBag']);
?>
          <table class="table table-condensed table-striped">
            <thead>
              <tr>
                <th>Product</th>
                <th style="text-align: center;">Quantity</th>
                <th style="text-align: center;">Action</t>
                <th style="text-align: right;">Unit Price</th>
                <th style="text-align: right;">Price</th>
              </tr>
            </thead>
            <tbody>
<?php
  foreach($bagProd as $value) {
    if (in_array($value['product_id'], array_unique($_SESSION['shoppingBag']))) {
      $bagPrice[$value['product_id']] = $bagCount[$value['product_id']] * $value['product_price'];
      if (($bagCount[$value['product_id']]+1) <= $value['product_quantity']) {
        $inc_btn_href  = " href=\"".$_SERVER['PHP_SELF']."?action=inc&pid=".$value['product_id']."\"";
        $inc_btn_state = "";
        $inc_btn_title = "Increase";
      } else {
        $inc_btn_href  = "";
        $inc_btn_state = " disabled";
        $inc_btn_title = "Reached maximum";
      }
      if (($bagCount[$value['product_id']]-1) > 0) {
        $dec_btn_href  = " href=\"".$_SERVER['PHP_SELF']."?action=dec&pid=".$value['product_id']."\"";
        $dec_btn_state = "";
        $dec_btn_title = "Decrease";
      } else {
        $dec_btn_href  = "";
        $dec_btn_state = " disabled";
        $dec_btn_title = "Reached minimum";
      }
      $rem_btn_href  = " href=\"".$_SERVER['PHP_SELF']."?action=rem&pid=".$value['product_id']."\"";
      $rem_btn_state = "";
      $rem_btn_title = "Remove";
?>
              <tr>
                <td><?php echo $value['product_name']; ?></td>
                <td style="text-align: center;"><?php echo $bagCount[$value['product_id']]."/".$value['product_quantity']; ?></td>
                <td style="text-align: center;">
                  <a<?php echo $inc_btn_href; ?> rel="tooltip" title="<?php echo $inc_btn_title; ?>" class="btn btn-mini btn-info<?php echo $inc_btn_state; ?>">&#43;</a>
                  <a<?php echo $dec_btn_href; ?> rel="tooltip" title="<?php echo $dec_btn_title; ?>" class="btn btn-mini btn-info<?php echo $dec_btn_state; ?>">&#45;</a>
                  <a<?php echo $rem_btn_href; ?> rel="tooltip" title="<?php echo $rem_btn_title; ?>" class="btn btn-mini btn-danger<?php echo $rem_btn_state; ?>">&#215;</a>
                </td>
                <td style="text-align: right;"><?php printf("%01.2f", $value['product_price']); ?></td>
                <td style="text-align: right;"><?php printf("%01.2f", $bagPrice[$value['product_id']]); ?></td>
              </tr>
<?php
    }
  }
?>
              <tr>
                <td colspan="4"><h4>TOTAL</h4></td>
<?php
  if (isset($bagPrice)) {
    $totPrice = array_sum($bagPrice);
  } else {
    $totPrice = 0;
  }
?>
                <td style="text-align: right;"><?php printf("%01.2f", $totPrice); ?></td>
              </tr>
            </tbody>
          </table>
          <div class="pull-right">
            <a href="<?php echo $_SERVER['PHP_SELF']."?do=order"; ?>" class="btn btn-success">Continue &raquo;</a>
          </div>
<?php
?>